package com.symbio.ishenapi.patient.controller;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.patient.bean.PatientBloodPressureResponse;
import com.symbio.ishenapi.patient.bean.PatientBloodSugar;
import com.symbio.ishenapi.patient.bean.PatientBloodSugarAttr;
import com.symbio.ishenapi.patient.model.*;
import com.symbio.ishenapi.patient.model.base.BaseTmPatientBloodPressure;
import com.symbio.ishenapi.patient.model.base.BaseTmPatientBloodSugar;
import io.jboot.web.controller.annotation.RequestMapping;

import java.math.BigInteger;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("/TmPatientBloodSugar")
public class TmPatientBloodSugarController extends BaseController {


    /**
     *
     *
     * @Author GuoShunYou
     * @Description 血糖数据添加录入
     * @date 下午 4:04   2018/10/24
     * @return void
     */
 /*   public void saveTmPatientBloodSugar(){      //血糖录入


    }*/

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 通过userID、runtime（日期）查询血糖数据
     * @date 下午 4:55   2018/10/24
     * @return void
     */
    public void findTmPatientBloodSugar(){
        PatientBloodSugar tmpbp = (PatientBloodSugar) getReqBodyObject(PatientBloodSugar.class);
        if(tmpbp.getRuntime()== null || "".equals(tmpbp.getRuntime())){
            Date date=new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            tmpbp.setRuntime(formatter.format(date));
        }
        PatientBloodSugar p = TmPatientBloodSugar.dao.findTmPatientBloodPressure(tmpbp);
        renderJsonRet(p);
    }


    /**
     *
     *
     * @Author GuoShunYou
     * @Description 通过userid查询所有血糖数据
     * @date 下午 5:32   2018/10/24
     * @return void
     */
    public void finALLSugar(){
        TmPatientBloodSugar tmpbp = (TmPatientBloodSugar) getReqBodyObject(TmPatientBloodSugar.class);
        List<PatientBloodPressureResponse> parespon=TmPatientBloodSugar.dao.findAllSugar(tmpbp);
        List<String> listruntime= TmPatientBloodSugar.dao.getRuntime(tmpbp);
        List alllimosis=new ArrayList();//空腹集合
        List allbreakfastLater=new ArrayList();//早餐后集合
        List alllunchBefore=new ArrayList();//午餐前
        List alllunchLater=new ArrayList();//午餐后
        List alldinnerBefore=new ArrayList();//晚餐前
        List alldinnerLater=new ArrayList();//晚餐后
        List allsleepBefore=new ArrayList();//睡觉前

        System.out.print("返回runtime集合"+listruntime);
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<listruntime.size();i++){
            /*String height=TmPatientBloodPressure.dao.getHeight(listruntime.get(i),tmpbp.getUserId());
            String low=TmPatientBloodPressure.dao.getLow(listruntime.get(i),tmpbp.getUserId());*/
            PatientBloodSugar tmpbpsud=new PatientBloodSugar();
            tmpbpsud.setUserId(tmpbp.getUserId());
            String timess=listruntime.get(i);
            tmpbpsud.setRuntime(timess);
            PatientBloodSugar sugartOne=TmPatientBloodSugar.dao.findTmPatientBloodPressure(tmpbpsud);
            System.out.println("list长度："+sugartOne.getList().size());





            for(int ij=0;ij<sugartOne.getList().size();ij++){
                List limosis=new ArrayList();//空腹集合
                List breakfastLater=new ArrayList();//早餐后集合
                List lunchBefore=new ArrayList();//午餐前
                List lunchLater=new ArrayList();//午餐后
                List dinnerBefore=new ArrayList();//晚餐前
                List dinnerLater=new ArrayList();//晚餐后
                List sleepBefore=new ArrayList();//睡觉前
                limosis.add(0,listruntime.get(i));
                limosis.add(1,sugartOne.getList().get(ij).getLimosis());

                alllimosis.add(limosis);

                breakfastLater.add(0,listruntime.get(i));
                breakfastLater.add(1,sugartOne.getList().get(ij).getBreakfastLater());

                allbreakfastLater.add(breakfastLater);

                lunchBefore.add(0,listruntime.get(i));
                lunchBefore.add(1,sugartOne.getList().get(ij).getLunchBefore());

                alllunchBefore.add(lunchBefore);

                lunchLater.add(0,listruntime.get(i));
                lunchLater.add(1,sugartOne.getList().get(ij).getLunchLater());

                alllunchLater.add(lunchLater);

                dinnerBefore.add(0,listruntime.get(i));
                dinnerBefore.add(1,sugartOne.getList().get(ij).getDinnerBefore());

                alldinnerBefore.add(dinnerBefore);

                dinnerLater.add(0,listruntime.get(i));
                dinnerLater.add(1,sugartOne.getList().get(ij).getDinnerLater());

                alldinnerLater.add(dinnerLater);

                sleepBefore.add(0,listruntime.get(i));
                sleepBefore.add(1,sugartOne.getList().get(ij).getSleepBefore());

                allsleepBefore.add(sleepBefore);
            }


            /*List one=new ArrayList();
            List two=new ArrayList();
            h.add(0,listruntime.get(i));
            h.add(1,height);
            allHeight.add(h);
            l.add(0,listruntime.get(i));
            l.add(1,low);
            allLow.add(l);*/
            try{
                Date qian,hou;
                if(i+1<listruntime.size()){
                    qian=sdfs.parse( listruntime.get(i));
                    hou=sdfs.parse( listruntime.get(i+1));
                    System.out.println(hou+"  "+qian+"   ");
                    Long cha=(hou.getTime()-qian.getTime())/1000/60;
                    for( int j =0;j<cha/1440-1;j++){
                        Calendar c = Calendar.getInstance();
                        c.setTime(qian);
                        c.add(Calendar.DAY_OF_MONTH,1);
                        Date tom=c.getTime();
                        qian=tom;
                        List zerone=new ArrayList();
                        zerone.add(0,f.format(qian));
                        zerone.add(1,"0");
                        allbreakfastLater.add(zerone);

                        List zertwo=new ArrayList();
                        zertwo.add(0,f.format(qian));
                        zertwo.add(1,"0");
                        alllunchBefore.add(zertwo);

                        List zerthree=new ArrayList();
                        zerthree.add(0,f.format(qian));
                        zerthree.add(1,"0");
                        alllunchLater.add(zerthree);

                        List zerfour=new ArrayList();
                        zerfour.add(0,f.format(qian));
                        zerfour.add(1,"0");
                        alldinnerBefore.add(zerfour);

                        List zerfive=new ArrayList();
                        zerfive.add(0,f.format(qian));
                        zerfive.add(1,"0");
                        alldinnerLater.add(zerfive);

                        List zersix=new ArrayList();
                        zersix.add(0,f.format(qian));
                        zersix.add(1,"0");
                        allsleepBefore.add(zersix);

                        List zerseven=new ArrayList();
                        zerseven.add(0,f.format(qian));
                        zerseven.add(1,"0");
                        alllimosis.add(zerseven);

                        /*allHeight.add(zerh);
                        allLow.add(zerl);*/
                    }
                }

            }catch (ParseException  e){

            }
        }
        System.out.println("limosis："+alllimosis);
        System.out.println("breakelater:"+allbreakfastLater);
        System.out.println("lunchbefore："+alllunchBefore);
        System.out.println("lunchlater:"+alllunchLater);
        System.out.println("dinnerbefore："+alldinnerBefore);
        System.out.println("dinnerlater:"+alldinnerLater);
        System.out.println("sleepbefore:"+allsleepBefore);
        Map<String,List> maps=new HashMap<String,List>();
        maps.put("limosis",alllimosis);
        maps.put("breakfastLater",allbreakfastLater);
        maps.put("lunchBefore",alllunchBefore);
        maps.put("lunchLater",alllunchLater);
        maps.put("dinnerBefore",alldinnerBefore);
        maps.put("dinnerLater",alldinnerLater);
        maps.put("sleepBefore",allsleepBefore);
        System.out.println(maps.toString());
        renderJsonRet(maps);
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 添加/修改血糖
     * @date 下午 7:22   2018/10/24
     * @return void
     */
    public void updateSugar(){
        PatientBloodSugar pab=(PatientBloodSugar)getReqBodyObject(PatientBloodSugar.class);
        int num=TmPatientBloodSugar.dao.selectCount(pab);
        if(num==0){
            //添加血糖主表记录
            TmPatientBloodSugar tpb= new TmPatientBloodSugar();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try{
                String time=pab.getRuntime();
                System.out.println(pab.getRuntime());
                System.out.println(sdf.parse(time));
                tpb.setRuntime(sdf.parse(time));
                tpb.setUserId(pab.getUserId());
                tpb.setCreateTime(new Date());
            }catch (ParseException e){

            }
            tpb.save();
            //添加血糖附表数据
            for (int i = 0; i < pab.getList().size(); i++) {
                //空腹
                TmPatientBloodSugarAttr tpblimosis = new TmPatientBloodSugarAttr();
                tpblimosis.setSugarId(tpb.getId());
                tpblimosis.setSugarType("200000027");
                if(pab.getList().get(i).getLimosis()==null || "".equals(pab.getList().get(i).getLimosis())){
                    tpblimosis.setSugarValue(0);
                }else{
                    tpblimosis.setSugarValue(Integer.valueOf(pab.getList().get(i).getLimosis()));
                }
                tpblimosis.setTimequantum(pab.getList().get(i).getTime());
                tpblimosis.save();

                //早餐后
                TmPatientBloodSugarAttr breakfastLater = new TmPatientBloodSugarAttr();
                breakfastLater.setSugarId(tpb.getId());
                breakfastLater.setSugarType("200000028");
                if(pab.getList().get(i).getBreakfastLater()==null || "".equals(pab.getList().get(i).getBreakfastLater())){
                    breakfastLater.setSugarValue(0);
                }else{
                    breakfastLater.setSugarValue(Integer.valueOf(pab.getList().get(i).getBreakfastLater()));
                }
                breakfastLater.setTimequantum(pab.getList().get(i).getTime());
                breakfastLater.save();
                //午餐前
                TmPatientBloodSugarAttr lunchBefore = new TmPatientBloodSugarAttr();
                lunchBefore.setSugarId(tpb.getId());
                lunchBefore.setSugarType("200000029");
                if(pab.getList().get(i).getLunchBefore()==null || "".equals(pab.getList().get(i).getLunchBefore())){
                    lunchBefore.setSugarValue(0);
                }else {
                    lunchBefore.setSugarValue(Integer.valueOf(pab.getList().get(i).getLunchBefore()));
                }
                lunchBefore.setTimequantum(pab.getList().get(i).getTime());
                lunchBefore.save();
                //午餐后
                TmPatientBloodSugarAttr lunchLater = new TmPatientBloodSugarAttr();
                lunchLater.setSugarId(tpb.getId());
                lunchLater.setSugarType("200000030");
                if(pab.getList().get(i).getLunchLater()==null || "".equals(pab.getList().get(i).getLunchLater())){
                    lunchLater.setSugarValue(0);
                }else{
                    lunchLater.setSugarValue(Integer.valueOf(pab.getList().get(i).getLunchLater()));
                }
                lunchLater.setTimequantum(pab.getList().get(i).getTime());
                lunchLater.save();
                //晚餐前
                TmPatientBloodSugarAttr dinnerBefore = new TmPatientBloodSugarAttr();
                dinnerBefore.setSugarId(tpb.getId());
                dinnerBefore.setSugarType("200000031");
                if(pab.getList().get(i).getDinnerBefore()==null || "".equals(pab.getList().get(i).getDinnerBefore())){
                    dinnerBefore.setSugarValue(0);
                }else{
                    dinnerBefore.setSugarValue(Integer.valueOf(pab.getList().get(i).getDinnerBefore()));
                }
                dinnerBefore.setTimequantum(pab.getList().get(i).getTime());
                dinnerBefore.save();
                //晚餐后
                TmPatientBloodSugarAttr dinnerLater = new TmPatientBloodSugarAttr();
                dinnerLater.setSugarId(tpb.getId());
                dinnerLater.setSugarType("200000032");
                if(pab.getList().get(i).getDinnerLater()==null || "".equals(pab.getList().get(i).getDinnerLater())){
                    dinnerLater.setSugarValue(0);
                }else{
                    dinnerLater.setSugarValue(Integer.valueOf(pab.getList().get(i).getDinnerLater()));
                }
                dinnerLater.setTimequantum(pab.getList().get(i).getTime());
                dinnerLater.save();
                //睡觉前
                TmPatientBloodSugarAttr sleepBefore = new TmPatientBloodSugarAttr();
                sleepBefore.setSugarId(tpb.getId());
                sleepBefore.setSugarType("200000033");
                if(pab.getList().get(i).getSleepBefore()==null || "".equals(pab.getList().get(i).getSleepBefore())){
                    sleepBefore.setSugarValue(0);
                }else{
                    sleepBefore.setSugarValue(Integer.valueOf(pab.getList().get(i).getSleepBefore()));
                }
                sleepBefore.setTimequantum(pab.getList().get(i).getTime());
                sleepBefore.save();
            }

            renderJsonError(40001, "血糖信息");
        }else{
            BigInteger id=TmPatientBloodSugar.dao.selectId(pab);

            //进行逻辑删除
            TmPatientBloodSugar tmp=new TmPatientBloodSugar();
            tmp.setId(id);
            tmp.setStatus("0");
            tmp.update();

            List<String> list=TmPatientBloodSugar.dao.getIdList(id);
            for(int i=0;i<list.size();i++){
                TmPatientBloodSugarAttr tmpb=new TmPatientBloodSugarAttr();
                tmpb.setId(new BigInteger(list.get(i)));
                tmpb.setStatus("0");
                tmpb.update();
            }

            //添加血糖主表记录
            TmPatientBloodSugar tpb= new TmPatientBloodSugar();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try{
                String time=pab.getRuntime();
                System.out.println(pab.getRuntime());
                System.out.println(sdf.parse(time));
                tpb.setRuntime(sdf.parse(time));
                tpb.setUserId(pab.getUserId());
                tpb.setCreateTime(new Date());
            }catch (ParseException e){

            }
            boolean b=tpb.save();
            int count=0;
            //添加血糖附表数据
            for (int i = 0; i < pab.getList().size(); i++) {
                //空腹
                TmPatientBloodSugarAttr tpblimosis = new TmPatientBloodSugarAttr();
                tpblimosis.setSugarId(tpb.getId());
                tpblimosis.setSugarType("200000027");
                if(pab.getList().get(i).getLimosis()==null || "".equals(pab.getList().get(i).getLimosis())){
                    tpblimosis.setSugarValue(0);
                }else{
                    tpblimosis.setSugarValue(Integer.valueOf(pab.getList().get(i).getLimosis()));
                }
                tpblimosis.setTimequantum(pab.getList().get(i).getTime());
                boolean limB=tpblimosis.save();
                if(limB){
                    count++;
                }

                //早餐后
                TmPatientBloodSugarAttr breakfastLater = new TmPatientBloodSugarAttr();
                breakfastLater.setSugarId(tpb.getId());
                breakfastLater.setSugarType("200000028");
                if(pab.getList().get(i).getBreakfastLater()==null || "".equals(pab.getList().get(i).getBreakfastLater())){
                    breakfastLater.setSugarValue(0);
                }else{
                    breakfastLater.setSugarValue(Integer.valueOf(pab.getList().get(i).getBreakfastLater()));
                }
                breakfastLater.setTimequantum(pab.getList().get(i).getTime());
                boolean breB=breakfastLater.save();
                if(breB){
                    count++;
                }

                //午餐前
                TmPatientBloodSugarAttr lunchBefore = new TmPatientBloodSugarAttr();
                lunchBefore.setSugarId(tpb.getId());
                lunchBefore.setSugarType("200000029");
                if(pab.getList().get(i).getLunchBefore()==null || "".equals(pab.getList().get(i).getLunchBefore())){
                    lunchBefore.setSugarValue(0);
                }else{
                    lunchBefore.setSugarValue(Integer.valueOf(pab.getList().get(i).getLunchBefore()));
                }
                lunchBefore.setTimequantum(pab.getList().get(i).getTime());
                boolean lunbe=lunchBefore.save();
                if(lunbe){
                    count++;
                }

                //午餐后
                TmPatientBloodSugarAttr lunchLater = new TmPatientBloodSugarAttr();
                lunchLater.setSugarId(tpb.getId());
                lunchLater.setSugarType("200000030");
                if(pab.getList().get(i).getLunchLater()==null || "".equals(pab.getList().get(i).getLunchLater())){
                    lunchLater.setSugarValue(0);
                }else{
                    lunchLater.setSugarValue(Integer.valueOf(pab.getList().get(i).getLunchLater()));
                }
                lunchLater.setTimequantum(pab.getList().get(i).getTime());
                boolean lunla=lunchLater.save();
                if (lunla){
                    count++;
                }

                //晚餐前
                TmPatientBloodSugarAttr dinnerBefore = new TmPatientBloodSugarAttr();
                dinnerBefore.setSugarId(tpb.getId());
                dinnerBefore.setSugarType("200000031");
                if(pab.getList().get(i).getDinnerBefore()==null || "".equals(pab.getList().get(i).getDinnerBefore())){
                    dinnerBefore.setSugarValue(0);
                }else {
                    dinnerBefore.setSugarValue(Integer.valueOf(pab.getList().get(i).getDinnerBefore()));
                }
                dinnerBefore.setTimequantum(pab.getList().get(i).getTime());
                boolean dinbe=dinnerBefore.save();
                if(dinbe){
                    count++;
                }

                //晚餐后
                TmPatientBloodSugarAttr dinnerLater = new TmPatientBloodSugarAttr();
                dinnerLater.setSugarId(tpb.getId());
                dinnerLater.setSugarType("200000032");
                if(pab.getList().get(i).getDinnerLater()==null || "".equals(pab.getList().get(i).getDinnerLater())){
                    dinnerLater.setSugarValue(0);
                }else{
                    dinnerLater.setSugarValue(Integer.valueOf(pab.getList().get(i).getDinnerLater()));
                }
                dinnerLater.setTimequantum(pab.getList().get(i).getTime());
                boolean dinla=dinnerLater.save();
                if(dinla){
                    count++;
                }

                //睡觉前
                TmPatientBloodSugarAttr sleepBefore = new TmPatientBloodSugarAttr();
                sleepBefore.setSugarId(tpb.getId());
                sleepBefore.setSugarType("200000033");
                if(pab.getList().get(i).getSleepBefore()==null || "".equals(pab.getList().get(i).getSleepBefore())){
                    sleepBefore.setSugarValue(0);
                }else{
                    sleepBefore.setSugarValue(Integer.valueOf(pab.getList().get(i).getSleepBefore()));
                }
                sleepBefore.setTimequantum(pab.getList().get(i).getTime());
                boolean sle=sleepBefore.save();
                if(sle){
                    count++;
                }

            }

            if(count/7==pab.getList().size() && b){
                //添加成功进行物理删除以上逻辑数据
                tmp.delete();
                for(int i=0;i<list.size();i++){
                    TmPatientBloodSugarAttr tmpb=new TmPatientBloodSugarAttr();
                    tmpb.setId(new BigInteger(list.get(i)));
                    tmpb.setStatus("0");
                    tmpb.delete();
                }



            }

            renderJsonError(40001, "血糖信息");
        }


    }

    /**
     * wenjun
     * 查询血糖最新一条数据
     */
    public void findOneSugar(){
        TmPatientBloodSugar user = (TmPatientBloodSugar)getReqBodyObject(TmPatientBloodSugar.class);
        List<TmPatientBloodSugar>list=TmPatientBloodSugar.dao.findOneSugar(user);
        renderJsonRet(list);
    }


    /**
     * 血糖列表，管理端用
     *
     */
    public void findSugarList(){
        String userId = getPara("userId");
        if(userId == null || "".equals(userId)){
            renderJsonError(40003, "患者编号");
            return;
        }
        Map map = TmPatientBloodPressure.dao.findPatientBloodPressure(new BigInteger(userId),getQueryModel());
        renderPageJsonRet(map.get("count"),map.get("data"));

    }

}
